package com.yunlankeji.stemcells.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.yunlankeji.ganxibaozhijia.R;
import com.yunlankeji.stemcells.activity.mine.Address_editActivity;
import com.yunlankeji.stemcells.model.request.Address;

import java.util.ArrayList;
import java.util.List;

public class AddressAdapter  extends RecyclerView.Adapter<AddressAdapter.MyViewHolder> implements View.OnClickListener{





    private List<com.yunlankeji.stemcells.model.request.Address> addressList;

    public AddressAdapter(List<com.yunlankeji.stemcells.model.request.Address> addressList) {
        this.addressList = addressList;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_adress,parent,false);
        MyViewHolder myViewHolder = new MyViewHolder(itemView);

        return myViewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        Address address = addressList.get(position);
        holder.name.setText(addressList.get(position).getAddress_name());
        holder.phone.setText(addressList.get(position).getAddress_phone());
        holder.address.setText(addressList.get(position).getAdress());


        holder.itemView.setTag(position);
        holder.lt_adress_edit.setTag(position);
        holder.lt_adress_item.setTag(position);
        holder.address_type.setTag(position);



    }


    @Override
    public int getItemCount() {
        return addressList.size();
    }

    @Override
    public void onClick(View v) {
        int position = (int) v.getTag();//getTag()获取数据
        if (mOnItemClickListener != null) {
            switch (v.getId()){
                case R.id.lt_address_edit:
                    mOnItemClickListener.onItemClick(v, ViewName.PRACTISE, position);
                    break;
                default:
                    mOnItemClickListener.onItemClick(v, ViewName.ITEM, position);
                    break;
            }
        }

    }

   public class MyViewHolder extends RecyclerView.ViewHolder {

        TextView name;
        TextView phone;
        TextView address_type;
        TextView address;
        LinearLayout lt_adress_edit,lt_adress_item;

        public MyViewHolder(View itemView) {
            super(itemView);
            this.name = itemView.findViewById(R.id.tv_address_name);
            this.phone = itemView.findViewById(R.id.tv_address_phone);
            this.address_type = itemView.findViewById(R.id.tv_address_Type);
            this.address = itemView.findViewById(R.id.tv_location_detail);
            this.lt_adress_edit = itemView.findViewById(R.id.lt_address_edit);
            this.lt_adress_item  = itemView.findViewById(R.id.lt_address_item);

            lt_adress_edit.setOnClickListener(AddressAdapter.this);
            lt_adress_item.setOnClickListener(AddressAdapter.this);

        }
    }

    public enum ViewName {
        ITEM,
        PRACTISE
    }

    public interface OnItemClickListener  {
        void onItemClick(View v, ViewName viewName, int position);
        void onItemLongClick(View v);
    }

    private OnItemClickListener mOnItemClickListener;//声明自定义的接口

    //定义方法并传给外面的使用者
    public void setOnItemClickListener(OnItemClickListener  listener) {
        this.mOnItemClickListener  = listener;
    }

}

